package com.rc.saas.tenant.interceptor.parser;

import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.StringValue;
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
import net.sf.jsqlparser.schema.Column;

public class SqlParserUtil {
    //private SubSelectProcess subSelectProcess;

    //private TenantConfiguration tenantConfiguration;

    /**
     * insert语句 添加插入租户id列
     *
     * @param expressionList 插入列的list
     * @throws Exception
     */
    //public static void addTenantValue(ExpressionList expressionList) throws Exception {
    //    for (Expression expression : expressionList.getExpressions()) {
    //        if (expression instanceof SubSelect) {
    //            SubSelectProcess.doSelect(expression);
    //        }
    //    }
    //
    //    //expressionList.getExpressions().add(new StringValue(tenantConfiguration.tenantInfo.getTenantId()));
    //    expressionList.getExpressions().add(new StringValue("TTT"));
    //}

    /**
     * 多条件情况下，使用AndExpression给where条件加上tenantid条件
     *
     * @param table 添加条件的表的名字
     * @param where 需要追加条件的where语句
     * @return
     * @throws Exception
     */
    public static AndExpression addAndExpression(String table, Expression where) {
        EqualsTo equalsTo = addEqualsTo(table);

        return new AndExpression(equalsTo, where);
    }

    /**
     * 创建一个EqualsTo相同判断
     *
     * @param table 表名
     * @return “A=B” 单个where条件表达式
     * @throws Exception
     */
    public static EqualsTo addEqualsTo(String table) {
        EqualsTo equalsTo = new EqualsTo();

        //equalsTo.setLeftExpression(new Column(table + '.' + tenantConfiguration.getTenantIdColumn()));
        //equalsTo.setRightExpression(new StringValue(tenantConfiguration.tenantInfo.getTenantId()));

        equalsTo.setLeftExpression(new Column(table + '.' + "tenant_code"));
        equalsTo.setRightExpression(new StringValue("TTT"));

        return equalsTo;
    }

    //@Resource
    //public void setTenantConfiguration(TenantConfiguration tenantConfiguration) {
    //    this.tenantConfiguration = tenantConfiguration;
    //}

    //@Resource
    //public void setSubSelectProcess(SubSelectProcess subSelectProcess) {
    //    this.subSelectProcess = subSelectProcess;
    //}
}
